using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;

namespace MatrixFramework.Shared.Contracts.Monitoring
{
    /// <summary>
    /// 指标服务接口
    /// 符合ABP vNext规范
    /// </summary>
    public interface IMetricsAppService : IApplicationService
    {
        /// <summary>
        /// 获取系统指标摘要
        /// </summary>
        /// <param name="input">查询参数</param>
        /// <returns>指标摘要</returns>
        Task<MetricsSummaryDto> GetSummaryAsync(GetMetricsInput input);

        /// <summary>
        /// 获取指标详情
        /// </summary>
        /// <param name="metricName">指标名称</param>
        /// <param name="input">查询参数</param>
        /// <returns>指标详情</returns>
        Task<MetricDetailsDto> GetDetailsAsync(string metricName, GetMetricDetailsInput input);

        /// <summary>
        /// 获取性能指标
        /// </summary>
        /// <param name="input">查询参数</param>
        /// <returns>性能指标</returns>
        Task<PerformanceMetricsDto> GetPerformanceMetricsAsync(GetPerformanceMetricsInput input);
    }

    /// <summary>
    /// 获取指标输入参数
    /// </summary>
    public class GetMetricsInput
    {
        public int Hours { get; set; } = 1;
        public string? MetricNameFilter { get; set; }
        public string? TagFilter { get; set; }
        public int MaxResultCount { get; set; } = 100;
        public int SkipCount { get; set; } = 0;
    }

    /// <summary>
    /// 获取指标详情输入参数
    /// </summary>
    public class GetMetricDetailsInput
    {
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
        public string? Aggregation { get; set; } = "avg";
        public int MaxResultCount { get; set; } = 1000;
    }

    /// <summary>
    /// 获取性能指标输入参数
    /// </summary>
    public class GetPerformanceMetricsInput
    {
        public int Hours { get; set; } = 24;
        public bool IncludeSystemMetrics { get; set; } = true;
        public bool IncludeApplicationMetrics { get; set; } = true;
    }

    /// <summary>
    /// 指标摘要DTO
    /// </summary>
    public class MetricsSummaryDto
    {
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
        public TimeSpan Period { get; set; }
        public Dictionary<string, MetricSummaryDto> Metrics { get; set; } = new();
        public int TotalMetrics { get; set; }
    }

    /// <summary>
    /// 单个指标摘要DTO
    /// </summary>
    public class MetricSummaryDto
    {
        public int Count { get; set; }
        public double Average { get; set; }
        public double Min { get; set; }
        public double Max { get; set; }
        public double Sum { get; set; }
        public string Unit { get; set; } = string.Empty;
    }

    /// <summary>
    /// 指标详情DTO
    /// </summary>
    public class MetricDetailsDto
    {
        public string MetricName { get; set; } = string.Empty;
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
        public List<DataPointDto> DataPoints { get; set; } = new();
        public Dictionary<string, string> Tags { get; set; } = new();
        public string Unit { get; set; } = string.Empty;
        public string Aggregation { get; set; } = string.Empty;
    }

    /// <summary>
    /// 数据点DTO
    /// </summary>
    public class DataPointDto
    {
        public DateTime Timestamp { get; set; }
        public double Value { get; set; }
    }

    /// <summary>
    /// 性能指标DTO
    /// </summary>
    public class PerformanceMetricsDto
    {
        public DateTime Timestamp { get; set; }
        public SystemMetricsDto System { get; set; } = new();
        public ApplicationMetricsDto Application { get; set; } = new();
        public List<CustomMetricDto> CustomMetrics { get; set; } = new();
    }

    /// <summary>
    /// 系统指标DTO
    /// </summary>
    public class SystemMetricsDto
    {
        public long MemoryUsageBytes { get; set; }
        public double MemoryUsageMB => MemoryUsageBytes / 1024.0 / 1024.0;
        public int ProcessorCount { get; set; }
        public long WorkingSetBytes { get; set; }
        public double WorkingSetMB => WorkingSetBytes / 1024.0 / 1024.0;
        public string OsVersion { get; set; } = string.Empty;
        public string MachineName { get; set; } = string.Empty;
    }

    /// <summary>
    /// 应用指标DTO
    /// </summary>
    public class ApplicationMetricsDto
    {
        public double UptimeSeconds { get; set; }
        public int ProcessId { get; set; }
        public int ThreadCount { get; set; }
        public long RequestCount { get; set; }
        public double AverageResponseTimeMs { get; set; }
        public double ErrorRate { get; set; }
    }

    /// <summary>
    /// 自定义指标DTO
    /// </summary>
    public class CustomMetricDto
    {
        public string Name { get; set; } = string.Empty;
        public double Value { get; set; }
        public string Unit { get; set; } = string.Empty;
        public Dictionary<string, string> Tags { get; set; } = new();
    }
}